<?php
namespace app\model;

use Lcobucci\JWT\Parser;
use Lcobucci\JWT\ValidationData;
use think\Model;
use Lcobucci\JWT\Builder;
use Lcobucci\JWT\Signer\Hmac\Sha256;
use Lcobucci\JWT\Signer\Key;

class Goods extends Model
{
    protected $name = 'goods';
    /**
     * 获取商品列表
     */
    public function  goodsList($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $pageSize = $param['page_size'] ?? 15 ;
        $page = $param['page'] ?? 1;
        $total = self::where(['is_del'=>0])->count();
        $list = self::where(['is_del'=>0])->page($page,$pageSize)->order('sort','desc')->order('id','desc')->select();
        $result = [
            'total'=>$total,
            'list'=>$list,
        ];
        return getSuccessResponse('查询成功',$result);
    }

    /**
     * 新增编辑商品
     */
    public function  addEditGoods($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $name = $param['name'] ?? '';
        $subtitle = $param['subtitle'] ?? '';
        $cate_id = $param['cate_id'] ?? '';
        $main_image = $param['main_image'] ?? '';
        $describe = $param['describe'] ?? '';
        $price = $param['price'] ?? '';
        $original_price = $param['original_price'] ?? $price;
        $discount = $param['discount'] ?? 1;
        $image_list = $param['image_list'] ?? '';
        $stock = $param['stock'] ?? 1;
        $goods_code = $param['goods_code'] ?? '';
        $content = $param['content'] ?? '';
        $is_show = $param['is_show'] ?? 1;
        $sort = $param['sort'] ?? 0;
        $id = $param['id'] ?? '';
        if (empty($name) || empty($cate_id) || empty($price) ||empty($main_image)){
            return getFailResponse('参数有误');
        }
        $data =[
            'name'=>$name,
            'subtitle'=>$subtitle,
            'cate_id'=>$cate_id,
            'main_image'=>$main_image,
            'describe'=>$describe,
            'price'=>$price,
            'original_price'=>$original_price,
            'discount'=>$discount,
            'image_list'=>$image_list,
            'stock'=>$stock,
            'goods_code'=>$goods_code,
            'content'=>$content,
            'is_show'=>$is_show,
            'sort'=>$sort,
            'create_time'=>time(),
            'update_time'=>time(),
        ];
        if(empty($id)){
            self::create($data);
        }else{
            unset($data['create_time']);
            self::where(['id'=>$id])->update($data);
        }
        return getSuccessResponse();
    }

    /**
     * 删除商品
     */
    public function  delGoods($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $ids = $param['ids'] ?? '';
        if (empty($ids)){
            return getFailResponse('参数有误');
        }
        self::where('id','in',$ids)->update(['is_del'=>1,'update_time'=>time()]);
        return getSuccessResponse('删除成功');
    }

    /**
     * 删除商品
     */
    public function  goodsDetail($param){
        $isLogin = isAdminLogin($param['admin_id']);
        if ($isLogin){
            return $isLogin;
        }
        $id = $param['id'] ?? '';
        if (empty($id)){
            return getFailResponse('参数有误');
        }
        $detail= self::where(['id'=>$id])->find();
        if (empty($detail)){
            return getFailResponse('商品不存在');
        }
        $detail['image_list'] = explode(',',$detail['image_list']);
        return getSuccessResponse('查询成功',$detail);
    }




}